from .models import DramaM3u8Link
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
import pymysql
from .config import DB_CONFIG

def insert_m3u8_link(drama_name, episode, m3u8_url):
    conn = pymysql.connect(**DB_CONFIG)
    cursor = conn.cursor()
    sql = "INSERT INTO drama_m3u8_links (drama_name, episode, m3u8_url) VALUES (%s, %s, %s)"
    cursor.execute(sql, (drama_name, episode, m3u8_url))
    conn.commit()
    cursor.close()
    conn.close()

def run_crawler(drama_list_url, episode_list_xpath, drama_name, m3u8_filename):
    print('run_crawler 已启动')
    options = Options()
    options.add_argument('--disable-gpu')
    service = Service('C:/chromedriver-win64/chromedriver.exe')  # 修改为你的chromedriver路径

    chrome_options = Options()
    chrome_options.add_argument('--headless') # 启用无头模式，不弹出浏览器窗口
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox') # 禁用沙盒模式，提高稳定性
    chrome_options.add_argument('--disable-dev-shm-usage') # 禁用/dev/shm使用，避免内存问题
    chrome_options.add_argument('--disable-blink-features=AutomationControlled') # 禁用自动化检测
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) # 排除自动化开关
    chrome_options.add_experimental_option('useAutomationExtension', False) # 禁用自动化扩展
    service = Service('C:/chromedriver-win64/chromedriver.exe')

    seleniumwire_options = {
        'exclude_hosts': ['mtg.mimit-pku.org']
    }

    driver = webdriver.Chrome(
        seleniumwire_options=seleniumwire_options,
        service=service,
        options=chrome_options
    )

    try:
        print('打开剧集列表页')
        driver.get(drama_list_url)
        time.sleep(3)
        print('查找分集列表')
        container = driver.find_element(By.XPATH, episode_list_xpath)
        children = container.find_elements(By.XPATH, './*')
        episode_links = []
        for child in children:
            if child.tag_name.lower() == 'a':
                href = child.get_attribute('href')
                episode = child.text.strip()
                if href:
                    episode_links.append((episode, href))
            else:
                try:
                    a = child.find_element(By.TAG_NAME, 'a')
                    href = a.get_attribute('href')
                    episode = a.text.strip()
                    if href:
                        episode_links.append((episode, href))
                except Exception:
                    continue
        episode_links = list(dict.fromkeys(episode_links))
        for episode, href in episode_links:
            print(f'分集: {episode}, 链接: {href}')
        for episode, href in episode_links:
            print(f'正在处理 {episode}: {href}')
            # 新建 driver (使用相同的headless配置)
            driver = webdriver.Chrome(
                seleniumwire_options=seleniumwire_options,
                service=service,
                options=chrome_options
            )
            driver.get(href)
            time.sleep(15)
            found = False
            with open('E:/videoSpider/backend/urls.log', 'a', encoding='utf-8') as f:
                f.write(f'\n==== {drama_name} {episode} 所有请求链接 ====\n')
                print('准备写入 urls.log')
                for request in driver.requests:
                    f.write(request.url + '\n')
                    print(request.url)
                    if request.response and request.url.endswith(m3u8_filename):
                        print(f'捕获到请求: {request.url}')
                        m3u8_url = request.url
                        try:
                            insert_m3u8_link(
                                drama_name=drama_name,
                                episode=episode,
                                m3u8_url=m3u8_url
                            )
                            print(f'已写入MySQL: {m3u8_url}')
                        except Exception as e:
                            print(f'写入MySQL失败: {e}')
                        found = True
                        break
                f.write('==== END ====\n')
            # video标签兜底
            if not found:
                try:
                    video = driver.find_element(By.TAG_NAME, 'video')
                    m3u8_url = video.get_attribute('src')
                    if m3u8_url and m3u8_url.endswith('.m3u8'):
                        insert_m3u8_link(drama_name, episode, m3u8_url)
                        print(f'兜底获取到 m3u8: {m3u8_url}')
                        found = True
                except Exception as e:
                    print('兜底获取 m3u8 失败:', e)
            if not found:
                print(f'{episode}未找到')
            driver.quit()
    except Exception as e:
        print('爬虫异常:', e)
    finally:
        driver.quit()
